package com.limm.dao.login;

import com.limm.util.DbUtil;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class LoginDao {

    private Connection conn = DbUtil.getConn();

    /**
     * 根据用户的邮箱来获取用户的uuid
     *
     * @param email:email
     * @return uuid
     */
    public String getId(String email) {
        try {
            String sql = "SELECT u_id FROM users WHERE u_email = ?";
            PreparedStatement state = conn.prepareStatement(sql);
            state.setString(1, email);
            ResultSet result = state.executeQuery();
            while (result.next()) {
                return result.getString("u_id");
            }
        } catch (Exception e) {

        }
        return "null";
    }

    public boolean isExist(String email, String password) {

        try {
            String sql = "SELECT u_email,u_password FROM users WHERE u_email = ?  and u_password = ?";
            PreparedStatement state = conn.prepareStatement(sql);
            state.setString(1, email);
            state.setString(2, password);
            ResultSet resultSet = state.executeQuery();
            while (resultSet.next()) {
                boolean usernameState = email.equals(resultSet.getString("u_email"));
                boolean passwordState = password.equals(resultSet.getString("u_password"));
                boolean result = usernameState && passwordState;
                if (result) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            System.out.println("登录查找失败");
        }
        return false;
    }
}
